﻿using System;
using System.ComponentModel;
using System.Security.Principal;
using Microsoft.Extensions.DependencyInjection;
using JESAI.Core.Modularity;
using JESAI.Authorize.Extensions;
using JESAI.Core.Configuration;
using JESAI.Framework;

namespace JESAI.Platform.WebApi;

/// <summary>
/// AspNetCore模块
/// </summary>
[Description("AspNetCore模块")]
public class AspNetCoreModule : JESAIModule
{
    /// <summary>
    /// 模块级别。级别越小越先启动
    /// </summary>
    public override ModuleLevel Level => ModuleLevel.Core;

    /// <summary>
    /// 模块启动顺序。模块启动的顺序先按级别启动，同一级别内部再按此顺序启动，
    /// </summary>
    public override int Order => 2;

    /// <summary>
    /// 添加服务。将模块服务添加到依赖注入服务容器中
    /// </summary>
    /// <param name="services">服务集合</param>
    public override IServiceCollection AddServices(IServiceCollection services)
    {
        //添加全局配置类
        services.AddSingleton(new Appsettings(services.GetConfiguration()));
        services.AddHttpContextAccessor();
        services.AddJESAIFramework();
        services.AddCurrentUserServer();

        // 注册代码页
        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
        return services;
    }

    /// <summary>
    /// 应用模块服务
    /// </summary>
    /// <param name="provider">服务提供程序</param>
    public override void UseModule(IServiceProvider provider) => Enabled = true;
}